package com.nanhai.buddhist.college.module.collegebase.service.classroominfo;

import cn.hutool.core.util.ObjectUtil;
import com.nanhai.buddhist.college.framework.common.util.collection.CollectionUtils;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.classes.ClassesInfoDO;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.departments.DepartmentsDO;
import com.nanhai.buddhist.college.module.collegebase.dal.mysql.classes.ClassesMapper;
import com.nanhai.buddhist.college.module.collegebase.dal.mysql.departments.DepartmentsMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.nanhai.buddhist.college.module.collegebase.controller.admin.classroominfo.vo.*;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.classroominfo.ClassroomInfoDO;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;

import com.nanhai.buddhist.college.module.collegebase.convert.classroominfo.ClassroomInfoConvert;
import com.nanhai.buddhist.college.module.collegebase.dal.mysql.classroominfo.ClassroomInfoMapper;

import static com.nanhai.buddhist.college.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.nanhai.buddhist.college.module.collegebase.enums.ErrorCodeConstants.*;

/**
 * 教室 Service 实现类
 *
 * @author 南海佛学院
 */
@Service
@Validated
public class ClassroomInfoServiceImpl implements ClassroomInfoService {

    @Resource
    private ClassroomInfoMapper classroomInfoMapper;


    @Resource
    private ClassesMapper classesMapper;
    @Resource
    private DepartmentsMapper departmentsMapper;

    @Override
    public String createClassroomInfo(ClassroomInfoCreateReqVO createReqVO) {
        // 插入
        ClassroomInfoDO classroomInfo = ClassroomInfoConvert.INSTANCE.convert(createReqVO);
        classroomInfoMapper.insert(classroomInfo);
        // 返回
        return classroomInfo.getId();
    }

    @Override
    public void updateClassroomInfo(ClassroomInfoUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateClassroomInfoExists(updateReqVO.getId());
        // 更新
        ClassroomInfoDO updateObj = ClassroomInfoConvert.INSTANCE.convert(updateReqVO);
        classroomInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteClassroomInfo(String id) {
        // 校验存在
        this.validateClassroomInfoExists(id);
        // 删除
        classroomInfoMapper.deleteById(id);
    }

    private void validateClassroomInfoExists(String id) {
        if (classroomInfoMapper.selectById(id) == null) {
            throw exception(CLASSROOM_INFO_NOT_EXISTS);
        }
    }

    @Override
    public ClassroomInfoDO getClassroomInfo(String id) {
        ClassroomInfoDO classroomInfoDO = classroomInfoMapper.selectById(id);
        if (ObjectUtil.isNull(classroomInfoDO)) {
            return null;
        }
         ClassesInfoDO classesInfoDOS = classesMapper.selectClassBaseInfoByClassesIdSelectOne(classroomInfoDO.getClassesId());
        /*DepartmentsDO departmentsDO = this.departmentsMapper.selectById(classroomInfoDO.getDepartmentId());*/
        classroomInfoDO.setCollegeId(classesInfoDOS.getCollegeId());
        classroomInfoDO.setProfessionId(classesInfoDOS.getProfessionsId());
        return classroomInfoDO;
    }

    @Override
    public List<ClassroomInfoDO> getClassroomInfoList(Collection<String> ids) {
        return CollectionUtils.isEmpty(ids) ? Collections.emptyList() : classroomInfoMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ClassroomInfoDO> getClassroomInfoPage(ClassroomInfoPageReqVO pageReqVO) {
        PageResult<ClassroomInfoDO> classroomInfoDOPageResult = classroomInfoMapper.selectPage(pageReqVO);
        Set<String> classesId = CollectionUtils.convertSet(classroomInfoDOPageResult.getList(), ClassroomInfoDO::getClassesId);
        Map<String, ClassesInfoDO> stringClassesInfoDOMap = Collections.EMPTY_MAP;
        if (Objects.nonNull(classesId) && !classesId.isEmpty()) {
            List<ClassesInfoDO> classesInfoDOS = classesMapper.selectClassBaseInfoByClassesId(classesId);
            stringClassesInfoDOMap = CollectionUtils.convertMap(classesInfoDOS, ClassesInfoDO::getClassesId);
        }


        Map<String, ClassesInfoDO> finalStringClassesInfoDOMap = stringClassesInfoDOMap;
        classroomInfoDOPageResult.getList().forEach(item -> {
            item.setDepartName(Optional.ofNullable(finalStringClassesInfoDOMap).map(s -> s.get(item.getClassesId())).map(ClassesInfoDO::getDepartmentsName).orElse(""));
            item.setCollegeName(Optional.ofNullable(finalStringClassesInfoDOMap).map(s -> s.get(item.getClassesId())).map(ClassesInfoDO::getCollegeName).orElse(""));

        });

        return classroomInfoDOPageResult;
    }

    @Override
    public List<ClassroomInfoDO> getClassroomInfoList(ClassroomInfoExportReqVO exportReqVO) {
        return classroomInfoMapper.selectList(exportReqVO);
    }

}
